package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_wjjhl.domain.CjlkljpsyBaDetail;
import com.ruoyi.report_wjjhl.domain.vo.CjlkljpsyBaDetailVo;
import com.ruoyi.report_wjjhl.mapper.CjlkljpsyBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.CjlkljpbgBsInfoBo;
import com.ruoyi.report_wjjhl.domain.vo.CjlkljpbgBsInfoVo;
import com.ruoyi.report_wjjhl.domain.CjlkljpbgBsInfo;
import com.ruoyi.report_wjjhl.mapper.CjlkljpbgBsInfoMapper;
import com.ruoyi.report_wjjhl.service.ICjlkljpbgBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 粗集料颗粒级配试验检测报告Service业务层处理
 *
 * @author zyb
 * @date 2024-08-08
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class CjlkljpbgBsInfoServiceImpl implements ICjlkljpbgBsInfoService {

    private final CjlkljpbgBsInfoMapper baseMapper;
    private final CjlkljpsyBaDetailMapper detailMapper;

    /**
     * 查询粗集料颗粒级配试验检测报告
     */
    @Override
    @FIllBasicInfo
    public CjlkljpbgBsInfoVo queryById(Long excelId){
        CjlkljpbgBsInfoVo cjlkljpbgBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<CjlkljpsyBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<CjlkljpsyBaDetail>()
            .eq(CjlkljpsyBaDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            cjlkljpbgBsInfoVo.setBaDetailVos(baDetailVos);
        }
        // 如果已经有值了则不需要计算结论。
        if (StrUtil.isNotEmpty(cjlkljpbgBsInfoVo.getJiancjl()) && !"检测结论：".equals(cjlkljpbgBsInfoVo.getJiancjl())) {
            return cjlkljpbgBsInfoVo;
        }
        return jcjl(cjlkljpbgBsInfoVo);
    }

    /**
     * 查询粗集料颗粒级配试验检测报告列表
     */
    @Override
    public TableDataInfo<CjlkljpbgBsInfoVo> queryPageList(CjlkljpbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CjlkljpbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<CjlkljpbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询粗集料颗粒级配试验检测报告列表
     */
    @Override
    public List<CjlkljpbgBsInfoVo> queryList(CjlkljpbgBsInfoBo bo) {
        LambdaQueryWrapper<CjlkljpbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CjlkljpbgBsInfo> buildQueryWrapper(CjlkljpbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CjlkljpbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, CjlkljpbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, CjlkljpbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), CjlkljpbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), CjlkljpbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), CjlkljpbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), CjlkljpbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), CjlkljpbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), CjlkljpbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), CjlkljpbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), CjlkljpbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancrq()), CjlkljpbgBsInfo::getJiancrq, bo.getJiancrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), CjlkljpbgBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), CjlkljpbgBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), CjlkljpbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), CjlkljpbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), CjlkljpbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getQuywz()), CjlkljpbgBsInfo::getQuywz, bo.getQuywz());
        lqw.eq(StringUtils.isNotBlank(bo.getWeitrwbh()), CjlkljpbgBsInfo::getWeitrwbh, bo.getWeitrwbh());
        lqw.eq(StringUtils.isNotBlank(bo.getTuycd()), CjlkljpbgBsInfo::getTuycd, bo.getTuycd());
        lqw.eq(StringUtils.isNotBlank(bo.getDaibsl()), CjlkljpbgBsInfo::getDaibsl, bo.getDaibsl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), CjlkljpbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), CjlkljpbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), CjlkljpbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), CjlkljpbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), CjlkljpbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), CjlkljpbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), CjlkljpbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), CjlkljpbgBsInfo::getPandyjIds, bo.getPandyjIds());
        return lqw;
    }

    /**
     * 新增粗集料颗粒级配试验检测报告
     */
    @Override
    public Boolean insertByBo(CjlkljpbgBsInfoBo bo) {
        CjlkljpbgBsInfo add = BeanUtil.toBean(bo, CjlkljpbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 14次
            long size = 14;
            ArrayList<CjlkljpsyBaDetail> details = new ArrayList<>((int) size);
            for (long i = 1; i <= size; i++) {
                details.add(new CjlkljpsyBaDetail(add.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改粗集料颗粒级配试验检测报告
     */
    @Override
    @ComputeReportResult
    public Boolean updateByBo(CjlkljpbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        CjlkljpbgBsInfo update = BeanUtil.toBean(bo, CjlkljpbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), CjlkljpsyBaDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(CjlkljpbgBsInfoBo bo) {
        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
            for (CjlkljpsyBaDetailVo detailVo : bo.getBaDetailVos()) {
                if (BigDecimalUtil.defaultValue(detailVo.getTonggbflPjz()).compareTo(BigDecimalUtil.defaultValue(detailVo.getZuidz())) >= 0 ||
                    BigDecimalUtil.defaultValue(detailVo.getTonggbflPjz()).compareTo(BigDecimalUtil.defaultValue(detailVo.getZuixz())) <= 0 ) {
                    bo.setExcelResult(2);
                    return;
                }
            }
            bo.setExcelResult(1);
        }
        bo.setExcelResult(0);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CjlkljpbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除粗集料颗粒级配试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 生成 粗集料颗粒级配试验检测报告 检测结论
     */
    @Override
    public CjlkljpbgBsInfoVo jcjl(CjlkljpbgBsInfoVo vo) {
        //        if (StrUtil.isEmpty(vo.getPandyj())) {
//            return vo;
//        }
        vo.setJiancjl("检测结论：经检测，该混合料筛分符合《水泥稳定碎石基层技术规范》DB37/T 3577-2019及设计要求。");
        return vo;
    }
}
